package com.gxdf.extendedcontrols.gxdfclient.services;

import android.content.Context;
import com.gxdf.extendedcontrols.gxdfclient.components.GxdfConceptContainer;
import com.gxdf.extendedcontrols.gxdfclient.components.GxdfFieldFactory;
import com.gxdf.extendedcontrols.gxdfclient.components.GxdfForm;
import com.gxdf.extendedcontrols.gxdfclient.components.IGxdfItem;
import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.Form;

public class FormToGxForm {

	public FormToGxForm() {
		// TODO Auto-generated constructor stub
	}

	
	public static GxdfForm getGxForm (Context context, Form form){
		
		GxdfForm gxform = new GxdfForm();
		GxdfFieldFactory factory = GxdfFieldFactory.getInstance();
		IGxdfItem gxdfItem = null;
		String conceptoActual=null;
		GxdfConceptContainer container= null;
		
		gxform.setName(form.getFormName());
		gxform.setDescription(form.getDescription());
		gxform.setFormInstance(form.getInstanceName());
		
		for(int i=0;i<form.getFields().size();i++){
			
			
			Field f = form.getFields().get(i);
			String concepto = f.getConcepto();
			
			if(f.getConcepto().equals("")){				
				gxdfItem = factory.getField(context, f);
				gxform.addField(gxdfItem);
				
			}
			else{
				
				if(!(concepto.equals(conceptoActual))){					
					container = new GxdfConceptContainer(context,f.isConceptoCol(),f.getConcepto());
					conceptoActual = concepto;
					container.addField(f);					
				}
				else{
					container.addField(f);
					if(i+1==form.getFields().size() || !(form.getFields().get(i+1).getConcepto().equals(conceptoActual)) ){
						
						gxform.addField(container);
					}	
					
				}
			}
			
			
		}
		
		if(form.getFields().size() == 1 && container!=null)
			gxform.addField(container);
		
		return gxform;
	}

}
